package com.sz.stock.common;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

public class MyInterCeptor implements HandlerInterceptor{

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        Cookie[] cookie=request.getCookies();
        String str1="";
        String str2="";
        Map<String,String> map=new HashMap<>();
        if (cookie!=null){
            for (int i = 0; i < cookie.length; i++) {
                str1=cookie[i].getName();
                str2=cookie[i].getValue();
                map.put(str1,str2);
            }
        }
        if (!map.containsKey("stock_user_id")){
            response.sendRedirect("/login.html");
            return false;
        }else {
            String role=map.get("stock_user_role");
            if (role.equals("0")){
                response.sendRedirect("/openAccount.html");
                return false;
            }
        }
        return true;
    }
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
    }
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
    }

}
